<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
			<el-form-item label="订单" prop="orderId">
				<el-input v-model="queryParams.orderId" placeholder="请输入订单" clearable size="small"
					@keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="项目" prop="projectId">
				<el-select v-model="queryParams.projectId" placeholder="请选择项目" clearable filterable size="small">
					<el-option v-for="dict in projectIdOptions" :key="dict.dictValue" :label="dict.dictLabel"
						:value="dict.dictValue" />
				</el-select>
			</el-form-item>
			<el-form-item label="名称" prop="name">
				<el-input v-model="queryParams.name" placeholder="请输入名称" clearable size="small"
					@keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="计划号" prop="serialNo">
				<el-input v-model="queryParams.serialNo" placeholder="请输入计划号" clearable size="small"
					@keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="交货日期">
				<el-date-picker v-model="daterangeDeliveryDate" size="small" style="width: 240px"
					value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期"
					end-placeholder="结束日期"></el-date-picker>
			</el-form-item>
			<el-form-item label="申请人" prop="applyUser">
				<el-select v-model="queryParams.applyUser" placeholder="请选择申请人" clearable filterable size="small">
					<el-option v-for="dict in applyUserOptions" :key="dict.dictValue" :label="dict.dictLabel"
						:value="dict.dictValue" />
				</el-select>
			</el-form-item>
			<el-form-item label="申请时间">
				<el-date-picker v-model="daterangeApplyTime" size="small" style="width: 240px" value-format="yyyy-MM-dd"
					type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期">
				</el-date-picker>
			</el-form-item>
			<el-form-item label="申请状态" prop="applyStatus">
				<el-select v-model="queryParams.applyStatus" placeholder="请选择申请状态" clearable filterable size="small">
					<el-option v-for="dict in applyStatusOptions" :key="dict.dictValue" :label="dict.dictLabel"
						:value="dict.dictValue" />
				</el-select>
			</el-form-item>
			<el-form-item label="审核人" prop="auditUser">
				<el-select v-model="queryParams.auditUser" placeholder="请选择审核人" clearable filterable size="small">
					<el-option v-for="dict in auditUserOptions" :key="dict.dictValue" :label="dict.dictLabel"
						:value="dict.dictValue" />
				</el-select>
			</el-form-item>
			<el-form-item label="审核时间">
				<el-date-picker v-model="daterangeAuditTime" size="small" style="width: 240px" value-format="yyyy-MM-dd"
					type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期">
				</el-date-picker>
			</el-form-item>
			<el-form-item label="审核状态" prop="auditStatus">
				<el-select v-model="queryParams.auditStatus" placeholder="请选择审核状态" clearable filterable size="small">
					<el-option v-for="dict in auditStatusOptions" :key="dict.dictValue" :label="dict.dictLabel"
						:value="dict.dictValue" />
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
					v-hasPermi="['mesPlan:productPlan:add']">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
					v-hasPermi="['mesPlan:productPlan:edit']">修改</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
					@click="handleDelete" v-hasPermi="['mesPlan:productPlan:remove']">删除</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
					v-hasPermi="['mesPlan:productPlan:export']">导出</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="productPlanList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="编号" align="center" prop="id" />
			<el-table-column label="订单" align="center" prop="orderId" />
			<el-table-column label="项目" align="center" prop="projectId" :formatter="projectIdFormat" />
			<el-table-column label="名称" align="center" prop="name" />
			<el-table-column label="计划号" align="center" prop="serialNo" />
			<el-table-column label="交货日期" align="center" prop="deliveryDate" width="180">
				<template slot-scope="scope">
					<span>{{ parseTime(scope.row.deliveryDate, '{y}-{m}-{d}') }}</span>
				</template>
			</el-table-column>
			<el-table-column label="申请人" align="center" prop="applyUser" :formatter="applyUserFormat" />
			<el-table-column label="申请时间" align="center" prop="applyTime" width="180">
				<template slot-scope="scope">
					<span>{{ parseTime(scope.row.applyTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
				</template>
			</el-table-column>
			<el-table-column label="申请状态" align="center" prop="applyStatus" :formatter="applyStatusFormat" />
			<el-table-column label="审核人" align="center" prop="auditUser" :formatter="auditUserFormat" />
			<el-table-column label="审核时间" align="center" prop="auditTime" width="180">
				<template slot-scope="scope">
					<span>{{ parseTime(scope.row.auditTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
				</template>
			</el-table-column>
			<el-table-column label="审核状态" align="center" prop="auditStatus" :formatter="auditStatusFormat" />
			<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
				<template slot-scope="scope">
					<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
						v-hasPermi="['mesPlan:productPlan:edit']">修改</el-button>
					<el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
						v-hasPermi="['mesPlan:productPlan:remove']">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
			@pagination="getList" />

		<!-- 添加或修改生产计划对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="80px" :inline="true">
				<el-form-item label="订单" prop="orderId">
					<el-input v-model="form.orderId" placeholder="请输入订单" />
				</el-form-item>
				<el-form-item label="项目" prop="projectId">
					<el-select v-model="form.projectId" placeholder="请选择项目" clearable filterable>
						<el-option v-for="dict in projectIdOptions" :key="dict.dictValue" :label="dict.dictLabel"
							:value="parseInt(dict.dictValue)"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="名称" prop="name">
					<el-input v-model="form.name" placeholder="请输入名称" />
				</el-form-item>
				<el-form-item label="计划号" prop="serialNo">
					<el-input v-model="form.serialNo" placeholder="请输入计划号" />
				</el-form-item>
				<el-form-item label="交货日期" prop="deliveryDate">
					<el-date-picker clearable size="small" v-model="form.deliveryDate" type="date"
						value-format="yyyy-MM-dd" placeholder="选择交货日期">
					</el-date-picker>
				</el-form-item>
				<el-form-item label="申请人" prop="applyUser">
					<el-select v-model="form.applyUser" placeholder="请选择申请人" clearable filterable>
						<el-option v-for="dict in applyUserOptions" :key="dict.dictValue" :label="dict.dictLabel"
							:value="dict.dictValue"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="申请时间" prop="applyTime">
					<el-date-picker clearable size="small" v-model="form.applyTime" type="datetime"
						value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择申请时间">
					</el-date-picker>
				</el-form-item>
				<el-form-item label="申请状态" prop="applyStatus">
					<el-select v-model="form.applyStatus" placeholder="请选择申请状态" clearable filterable>
						<el-option v-for="dict in applyStatusOptions" :key="dict.dictValue" :label="dict.dictLabel"
							:value="dict.dictValue"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="审核人" prop="auditUser">
					<el-select v-model="form.auditUser" placeholder="请选择审核人" clearable filterable>
						<el-option v-for="dict in auditUserOptions" :key="dict.dictValue" :label="dict.dictLabel"
							:value="dict.dictValue"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="审核时间" prop="auditTime">
					<el-date-picker clearable size="small" v-model="form.auditTime" type="datetime"
						value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择审核时间">
					</el-date-picker>
				</el-form-item>
				<el-form-item label="审核状态" prop="auditStatus">
					<el-select v-model="form.auditStatus" placeholder="请选择审核状态" clearable filterable>
						<el-option v-for="dict in auditStatusOptions" :key="dict.dictValue" :label="dict.dictLabel"
							:value="dict.dictValue"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="审核意见" prop="auditComment">
					<el-input v-model="form.auditComment" type="textarea" placeholder="请输入内容" />
				</el-form-item>
				<el-form-item label="备注" prop="remark">
					<el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
				</el-form-item>
				<el-divider content-position="center">生产计划明细信息</el-divider>
				<el-row :gutter="10" class="mb8">
					<el-col :span="1.5">
						<el-button type="primary" icon="el-icon-plus" size="mini"
							@click="handleAddMesProductPlanDetail">添加</el-button>
					</el-col>
					<el-col :span="1.5">
						<el-button type="danger" icon="el-icon-delete" size="mini"
							@click="handleDeleteMesProductPlanDetail">删除</el-button>
					</el-col>
				</el-row>
				<el-table :data="mesProductPlanDetailList" :row-class-name="rowMesProductPlanDetailIndex"
					@selection-change="handleMesProductPlanDetailSelectionChange" ref="mesProductPlanDetail">
					<el-table-column type="selection" width="50" align="center" />
					<el-table-column label="序号" align="center" prop="index" width="50" />
					<el-table-column label="物料" prop="materialId" width="150">
						<template slot-scope="scope">
							<el-form-item :prop="'mesProductPlanDetailList.'+scope.$index+'.materialId'"
								:rules="rules.materialId">
								<el-input v-model="scope.row.materialId" placeholder="请输入物料" />
							</el-form-item>
						</template>
					</el-table-column>
					<el-table-column label="BOM单" prop="bomId" width="150">
						<template slot-scope="scope">
							<el-form-item :prop="'mesProductPlanDetailList.'+scope.$index+'.bomId'"
								:rules="rules.bomId">
								<el-input v-model="scope.row.bomId" placeholder="请输入BOM单" />
							</el-form-item>
						</template>
					</el-table-column>
					<el-table-column label="生产数量" prop="quantity" width="150">
						<template slot-scope="scope">
							<el-form-item :prop="'mesProductPlanDetailList.'+scope.$index+'.quantity'"
								:rules="rules.quantity">
								<el-input v-model="scope.row.quantity" placeholder="请输入生产数量" />
							</el-form-item>
						</template>
					</el-table-column>
					<el-table-column label="生产日期" prop="productDate" width="240">
						<template slot-scope="scope">
							<el-form-item :prop="'mesProductPlanDetailList.'+scope.$index+'.productDate'"
								:rules="rules.productDate">
								<el-date-picker clearable v-model="scope.row.productDate" type="date"
									value-format="yyyy-MM-dd" placeholder="请选择生产日期" />
							</el-form-item>
						</template>
					</el-table-column>
				</el-table>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {
		listProductPlan,
		getProductPlan,
		delProductPlan,
		addProductPlan,
		updateProductPlan,
		exportProductPlan
	} from "@/api/mesPlan/productPlan";

	export default {
		name: "ProductPlan",
		components: {},
		data() {
			return {
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 子表选中数据
				checkedMesProductPlanDetail: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				// 生产计划表格数据
				productPlanList: [],
				// 生产计划明细表格数据
				mesProductPlanDetailList: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				// 项目字典
				projectIdOptions: [],
				// 交货日期时间范围
				daterangeDeliveryDate: [],
				// 申请人字典
				applyUserOptions: [],
				// 申请时间时间范围
				daterangeApplyTime: [],
				// 申请状态字典
				applyStatusOptions: [],
				// 审核人字典
				auditUserOptions: [],
				// 审核时间时间范围
				daterangeAuditTime: [],
				// 审核状态字典
				auditStatusOptions: [],
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					orderId: null,
					projectId: null,
					name: null,
					serialNo: null,
					deliveryDate: null,
					applyUser: null,
					applyTime: null,
					applyStatus: null,
					auditUser: null,
					auditTime: null,
					auditStatus: null,
				},
				// 表单参数
				form: {},
				// 表单校验
				rules: {
					orderId: [{
						required: true,
						message: "订单不能为空",
						trigger: "blur"
					}],
					projectId: [{
						required: true,
						message: "项目不能为空",
						trigger: "change"
					}],
					name: [{
						required: true,
						message: "名称不能为空",
						trigger: "blur"
					}],
					deliveryDate: [{
						required: true,
						message: "交货日期不能为空",
						trigger: "blur"
					}],
					materialId: [{
						required: true,
						message: "物料不能为空",
						trigger: "blur"
					}],
					quantity: [{
						required: true,
						message: "生产数量不能为空",
						trigger: "blur"
					}],
					productDate: [{
						required: true,
						message: "生产日期不能为空",
						trigger: "blur"
					}],
				}
			};
		},
		created() {
			this.getList();
			this.getQueryData("query_project").then(res => {
				this.projectIdOptions = res.data;
			});
			this.getQueryData("query_user").then(res => {
				this.applyUserOptions = res.data;
			});
			this.getDicts("apply_status").then(response => {
				this.applyStatusOptions = response.data;
			});
			this.getQueryData("query_user").then(res => {
				this.auditUserOptions = res.data;
			});
			this.getDicts("audit_status").then(response => {
				this.auditStatusOptions = response.data;
			});
		},
		watch: {
			'mesProductPlanDetailList': {
				handler(newVal, oldVal) {
					this.form.mesProductPlanDetailList = newVal;
				},
				immediate: true,
				deep: true,
			}
		},
		methods: {
			/** 查询生产计划列表 */
			getList() {
				this.loading = true;
				this.queryParams.params = {};
				if (null != this.daterangeDeliveryDate && '' != this.daterangeDeliveryDate) {
					this.queryParams.params["beginDeliveryDate"] = this.daterangeDeliveryDate[0];
					this.queryParams.params["endDeliveryDate"] = this.daterangeDeliveryDate[1];
				}
				if (null != this.daterangeApplyTime && '' != this.daterangeApplyTime) {
					this.queryParams.params["beginApplyTime"] = this.daterangeApplyTime[0];
					this.queryParams.params["endApplyTime"] = this.daterangeApplyTime[1];
				}
				if (null != this.daterangeAuditTime && '' != this.daterangeAuditTime) {
					this.queryParams.params["beginAuditTime"] = this.daterangeAuditTime[0];
					this.queryParams.params["endAuditTime"] = this.daterangeAuditTime[1];
				}
				listProductPlan(this.queryParams).then(response => {
					this.productPlanList = response.rows;
					this.total = response.total;
					this.loading = false;
				});
			},
			// 项目字典翻译
			projectIdFormat(row, column) {
				return this.selectDictLabel(this.projectIdOptions, row.projectId);
			},
			// 申请人字典翻译
			applyUserFormat(row, column) {
				return this.selectDictLabel(this.applyUserOptions, row.applyUser);
			},
			// 申请状态字典翻译
			applyStatusFormat(row, column) {
				return this.selectDictLabel(this.applyStatusOptions, row.applyStatus);
			},
			// 审核人字典翻译
			auditUserFormat(row, column) {
				return this.selectDictLabel(this.auditUserOptions, row.auditUser);
			},
			// 审核状态字典翻译
			auditStatusFormat(row, column) {
				return this.selectDictLabel(this.auditStatusOptions, row.auditStatus);
			},
			// 取消按钮
			cancel() {
				this.open = false;
				this.reset();
			},
			// 表单重置
			reset() {
				this.form = {
					id: null,
					orderId: null,
					projectId: null,
					name: null,
					serialNo: null,
					deliveryDate: null,
					applyUser: null,
					applyTime: null,
					applyStatus: null,
					auditUser: null,
					auditTime: null,
					auditStatus: null,
					auditComment: null,
					remark: null,
					mesProductPlanDetailList: [],
				};
				this.mesProductPlanDetailList = [];
				this.resetForm("form");
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.daterangeDeliveryDate = [];
				this.daterangeApplyTime = [];
				this.daterangeAuditTime = [];
				this.resetForm("queryForm");
				this.handleQuery();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.id)
				this.single = selection.length !== 1
				this.multiple = !selection.length
			},
			/** 新增按钮操作 */
			handleAdd() {
				this.reset();
				this.open = true;
				this.title = "添加生产计划";
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				this.reset();
				const id = row.id || this.ids
				getProductPlan(id).then(response => {
					this.form = response.data;
					this.mesProductPlanDetailList = response.data.mesProductPlanDetailList;
					this.open = true;
					this.title = "修改生产计划";
				});
			},
			/** 提交按钮 */
			submitForm() {
				this.$refs["form"].validate(valid => {
					if (valid) {

						if (this.form.id != null) {
							updateProductPlan(this.form).then(response => {
								this.msgSuccess("修改成功");
								this.open = false;
								this.getList();
							});
						} else {
							addProductPlan(this.form).then(response => {
								this.msgSuccess("新增成功");
								this.open = false;
								this.getList();
							});
						}
					}
				});
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				const ids = row.id || this.ids;
				this.$confirm('是否确认删除生产计划编号为"' + ids + '"的数据项?', "警告", {
					confirmButtonText: "确定",
					cancelButtonText: "取消",
					type: "warning"
				}).then(function() {
					return delProductPlan(ids);
				}).then(() => {
					this.getList();
					this.msgSuccess("删除成功");
				})
			},
			/** 生产计划明细序号 */
			rowMesProductPlanDetailIndex({
				row,
				rowIndex
			}) {
				row.index = rowIndex + 1;
			},
			/** 生产计划明细添加按钮操作 */
			handleAddMesProductPlanDetail() {
				let obj = {};
				obj.materialId = "";
				obj.quantity = "";
				obj.productDate = "";
				this.mesProductPlanDetailList.push(obj);
			},
			/** 生产计划明细删除按钮操作 */
			handleDeleteMesProductPlanDetail() {
				if (this.checkedMesProductPlanDetail.length == 0) {
					this.$alert("请先选择要删除的生产计划明细数据", "提示", {
						confirmButtonText: "确定",
					});
				} else {
					this.mesProductPlanDetailList.splice(this.checkedMesProductPlanDetail[0].index - 1, 1);
				}
			},
			/** 单选框选中数据 */
			handleMesProductPlanDetailSelectionChange(selection) {
				if (selection.length > 1) {
					this.$refs.mesProductPlanDetail.clearSelection();
					this.$refs.mesProductPlanDetail.toggleRowSelection(selection.pop());
				} else {
					this.checkedMesProductPlanDetail = selection;
				}
			},
			/** 导出按钮操作 */
			handleExport() {
				const queryParams = this.queryParams;
				this.$confirm('是否确认导出所有生产计划数据项?', "警告", {
					confirmButtonText: "确定",
					cancelButtonText: "取消",
					type: "warning"
				}).then(function() {
					return exportProductPlan(queryParams);
				}).then(response => {
					this.download(response.msg);
				})
			}
		}
	};
</script>
